package org.pie.drawing.apps.picimageserver.utils;

import cn.hutool.core.util.StrUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class DownloadUtil {

    /**
     * 下载文件
     *
     * @param filePath
     * @param response
     */
    public static void download(String filePath, HttpServletResponse response) {
        if (!StrUtil.isEmpty(filePath)) {
            File file = new File(filePath);
            if (file.exists()) {
                byte[] buffer = null;
                InputStream is = null;
                OutputStream os = null;
                try {
                    is = new BufferedInputStream(new FileInputStream(filePath));
                    buffer = new byte[is.available()];
                    is.read(buffer);
                    response.reset();
                    response.addHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes(), StandardCharsets.UTF_8));
                    response.addHeader("Content-Length", file.length() + "");
                    os = new BufferedOutputStream(response.getOutputStream());
                    response.setContentType("application/octet-stream;charset=UTF-8");
                    os.write(buffer);
                    os.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    closeCloseable(os);
                    closeCloseable(is);
                }
            }
        }
    }

    public static void download(String filePath, String fileName, HttpServletResponse response) {
        if (!StrUtil.isEmpty(filePath)) {
            File file = new File(filePath);
            if (file.exists()) {
                byte[] buffer = null;
                InputStream is = null;
                OutputStream os = null;
                try {
                    is = new BufferedInputStream(new FileInputStream(filePath));
                    buffer = new byte[is.available()];
                    is.read(buffer);
                    response.reset();
                    response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), StandardCharsets.UTF_8));
                    response.addHeader("Content-Length", file.length() + "");
                    os = new BufferedOutputStream(response.getOutputStream());
                    response.setContentType("application/octet-stream;charset=UTF-8");
                    os.write(buffer);
                    os.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    closeCloseable(os);
                    closeCloseable(is);
                }
            }
        }
    }

    public static void closeCloseable(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}
